package aop.introduction;

import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.IntroductionInterceptor;

public class DoSomeIntroductionInterceptor implements IDoOther, IntroductionInterceptor {
	private static final Logger log = LoggerFactory
			.getLogger(DoSomeIntroductionInterceptor.class);

	public void doOther(SimpleBean bean) {
		bean.setLog(bean.getLog() + IDoOther.DO_OTHER_LOG_STR);
		log.debug(IDoOther.DO_OTHER_LOG_STR);
	}

	public Object invoke(MethodInvocation invocation) throws Throwable {
		if (implementsInterface(invocation.getMethod().getDeclaringClass())){
			return invocation.getMethod().invoke(this, invocation.getArguments());
		}else{
			return invocation.proceed();
		}
	}

	public boolean implementsInterface(Class<?> intf) {
		return intf.isAssignableFrom(IDoOther.class);
	}

}
